package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.auditflowpreset.AuditFlowPresetAddReqDTO;
import com.yixin.hospital.entity.dto.req.auditflowpreset.AuditFlowPresetEditReqDTO;
import com.yixin.hospital.entity.vo.AuditFlowPresetVO;
import com.yixin.hospital.entity.vo.JobDutyVO;
import com.yixin.hospital.service.AuditFlowPresetService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/audit-flow-preset")
@Api(value = "审批流管理", tags = "审批流管理")
public class AuditFlowPresetController {

    @Resource
    private AuditFlowPresetService auditFlowPresetService;

    @GetMapping("/list")
    @ApiOperation(value = "审批流列表")
    public ApiResult<List<AuditFlowPresetVO>> list() {
        List<AuditFlowPresetVO> resp = auditFlowPresetService.allList();
        return ApiResult.success(resp);
    }

    @GetMapping("/info")
    @ApiOperation(value = "审批流详情（带人员）")
    public ApiResult<List<JobDutyVO>> info(@RequestParam(value = "businessCode") String businessCode) {
        List<JobDutyVO> jobDutyList = auditFlowPresetService.getInfo(businessCode);
        return ApiResult.success(jobDutyList);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加审批流")
    public ApiResult add(@Valid @RequestBody AuditFlowPresetAddReqDTO auditFlowPresetAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        auditFlowPresetService.add(auditFlowPresetAddReqDTO, userName);
        return ApiResult.success();
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑审批流")
    public ApiResult edit(@Valid @RequestBody AuditFlowPresetEditReqDTO auditFlowPresetEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        auditFlowPresetService.edit(auditFlowPresetEditReqDTO, userName);
        return ApiResult.success();
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除审批流")
    public ApiResult remove(@PathVariable int id) {
        auditFlowPresetService.removeById(id);
        return ApiResult.success();
    }
}
